package com.luoran.bartalks.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.luoran.bartalks.core.UserContext;
import com.luoran.bartalks.dao.IBtPlanDetailsDao;
import com.luoran.bartalks.dao.IBtPlanEntryDao;
import com.luoran.bartalks.entity.biz.BtPlanDetails;
import com.luoran.bartalks.entity.biz.BtPlanEntry;
import com.luoran.bartalks.service.IBtPlanEntryService;

@Service
public class BtPlanEntryService implements IBtPlanEntryService {

	@Autowired
	private IBtPlanEntryDao planEntryDao;

	@Autowired
	private IBtPlanDetailsDao planDetailsDao;

	// 保存计划途径点.
	@Override
	public String addPlanEntry(BtPlanEntry planEntry, String planId) {
		// 新增保存
		planEntryDao.insertTemplate(planEntry);
		return planEntry.getId();
	}

	// 根据计划id查询途径点
	@Override
	public List<BtPlanEntry> getPlanEntryByPlanId(String planId, String companyId) {

		return planEntryDao.getPlanEntryByPlanId(planId, companyId);
	}

	// 每次更新需要删除之前保存的计划途径点记录.
	@Override
	public void deletePlanEntryByPlanId(String planId, String companyId) {
		planEntryDao.deletePlanEntryByPlanId(planId, UserContext.get().getCompanyId());

	}

	// 根据sign .planId 获取PlanEntry
	@Override
	public BtPlanEntry getPlanEntryByPlanIdAndSign(String planId, String companyId, int sign) {

		return planEntryDao.getPlanEntryByPlanIdAndSign(planId, companyId, sign);
	}

	// 根据planId,addressid获取PlanEntry
	@Override
	public BtPlanEntry getPlanEntryByPlanIdAndAddresId(String planId, String addressId, String companyId) {

		return planEntryDao.getPlanEntryByPlanIdAndAddresId(planId, addressId, companyId);
	}

	// 根据id获取PlanEntry
	@Override
	public BtPlanEntry getPlanEntryById(String Id) {
		return planEntryDao.single(Id);
	}

	@Transactional
	@Override
	public void updateExceptionInfo(String planEntryId, String planId, String info) {
		try {
			BtPlanEntry planEntry = new BtPlanEntry();
			planEntry.setId(planEntryId);
			planEntry.setException(info);
			planEntry.setStates(1);
			planEntryDao.updateTemplateById(planEntry);
			BtPlanDetails planDetails = new BtPlanDetails();
			planDetails.setId(planId);
			planDetails.setStatus(4);
			planDetailsDao.updateTemplateById(planDetails);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("更新计划异常失败！,请查看日志！");
		}
	}

	@Override
	public List<BtPlanEntry> getByPlanIdOfAPP(List<String> Ids, String companyId) {
		return planEntryDao.getByPlanIdOfAPP(Ids, companyId);
	}
	
	

}
